package com.wz.project.inStock.stockin.controller;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.wz.common.utils.ServletUtils;
import com.wz.project.bas.basJobId.service.IBasJobIdService;
import com.wz.project.inStock.stockin.domain.InStockinVo;
import com.wz.project.inStock.stockindetail.domain.InStockinDetail;
import com.wz.project.inStock.stockindetail.service.IInStockinDetailService;
import com.wz.project.sap.DN.service.IZflWmsGetDnService;
import com.wz.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.project.inStock.stockin.domain.InStockin;
import com.wz.project.inStock.stockin.service.IInStockinService;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.common.utils.poi.ExcelUtil;
import com.wz.framework.web.page.TableDataInfo;

/**
 * 入库单据Controller
 *
 * @author wz
 * @date 2022-09-07
 */
@Controller
@RequestMapping("/stockin/stockin")
public class InStockinController extends BaseController {
    private String prefix = "inStock";

    @Autowired
    private IInStockinService inStockinService;
    @Autowired
    private IInStockinDetailService inStockinDetailService;
    @Autowired
    private IBasJobIdService iBasJobIdService;

//    @RequiresPermissions("stockin:stockin:view")
    @GetMapping("/stockinManage")
    public String stockin() {
        return prefix + "/stockinManage";
    }

    @GetMapping("/addstockin")
    public String addstockin() {
        return prefix + "/addstockin";
    }

    @GetMapping("/selectSupplier")
    public String selectSupplier() {
        return prefix + "/selectSupplier";
    }

    @GetMapping("/selectPro")
    public String selectPro() {
        return prefix + "/selectPro";
    }

    /**
     * 查询入库单据列表
     */
//    @RequiresPermissions("stockin:stockin:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InStockin inStockin) {
        startPage();
        List<InStockin> list = inStockinService.selectInStockinList(inStockin);
        return getDataTable(list);
    }

    /**
     * 新建入库单
     */
    /**
     * 新增保存入库主单
     */
    @RequiresPermissions("mydo:stockout:add")
    @Log(title = "入库主单", businessType = BusinessType.INSERT)
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(@RequestBody InStockinVo inStockVo) {
        InStockin stockin = inStockVo.getStockin();
        List<InStockinDetail> stockinDetailList = inStockVo.getStockinDetailList();
        String inStockId = iBasJobIdService.getjobId("RK");
        String guid = UUID.randomUUID().toString();
        stockin.setStockinGuid(guid);
        stockin.setStockinId(inStockId);
        stockin.setStatus("1");
        stockin.setStockinSource("0");
        stockin.setWarehouseId("01");
        stockin.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //获取当前操作员
        User user = (User) ServletUtils.getSubject().getPrincipal();
        String userName = user.getUserName();
        stockin.setCreateManId(userName);
        inStockinService.insertInStockin(stockin);
        for (int i = 0; i < stockinDetailList.size(); i++) {
            stockinDetailList.get(i).setStockinDetailId(inStockId + "-" + (i + 1));
            stockinDetailList.get(i).setStockinId(inStockId);
            stockinDetailList.get(i).setStockinGuid(guid);
            stockinDetailList.get(i).setAssignNum(BigDecimal.ZERO);
            stockinDetailList.get(i).setPrice(BigDecimal.ZERO);
            inStockinDetailService.insertInStockinDetail(stockinDetailList.get(i));
        }

        return AjaxResult.success("保存成功",stockin.getStockinId());

    }

    /**
     * 单据审核
     */
    @RequiresPermissions("stockin:stockin:audit")
    @PostMapping( "/audit")
    @ResponseBody
    public AjaxResult audit(String ids)
    {
        int sa = inStockinService.audit(ids);
        if (sa == 1){
            return AjaxResult.success("审核成功！");
        }else{
            return AjaxResult.error("只能审核状态为入库开单的单据！");
        }
    }

    @RequiresPermissions("mydo:stockinmanage:receiptSure")
    @Log(title = "入库单据确认", businessType = BusinessType.OTHER)
    @PostMapping( "/receiptSure")
    @ResponseBody
    public AjaxResult receiptSure(String ids)
    {
        int sa = inStockinService.receiptSure(ids);
        if (sa == 1){
            return AjaxResult.success("确认成功！");
        }else{
            return AjaxResult.error("只能确认执行完成的单据！");
        }
    }

    /**
     * 入库单据作废
     * @param ids
     * @return
     */
//    @RequiresPermissions("mydo:stockinmanage:receiptCancle")
    @Log(title = "入库单据作废", businessType = BusinessType.OTHER)
    @PostMapping( "/receiptCancle")
    @ResponseBody
    public AjaxResult receiptCancle(String ids)
    {
        int sa = inStockinService.receiptCancle(ids);
        if (sa == 1){
            return AjaxResult.success("作废成功！");
        }else{
            return AjaxResult.error("只能作废未开始执行的单据！");
        }
    }
}
